package com.Dao;

import com.entity.User;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;


public class UserDao  extends BaseDao {
    public List<User> queryUsersById(Object...params) {
        String sql = "select * from user where uName = ? and uPass = ? ";
        try {
            return queryForListObject(sql, User.class, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<User> queryUsersByuName(Object...params) {
        String sql = "select * from user where uName = ? ";
        try {
            return queryForListObject(sql, User.class, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public  int InsertValues(Object...params){
        String sql="insert into user(uName, uPass, uType,uphone) values(?,?,?,?)";

        try {
            int i = queryRunner.update(sql, params);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public  List<User>  queryUserByBalance(Object...params){
        String sql="select balance from user where uName=?";
        try {
            return  queryForListObject(sql, User.class, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }





    public  int updateAddBalance(Object...params){
        String sql="update  user set balance = balance+? where uName=?";

        try {
            int i = queryRunner.update(sql, params);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public  int updateReduceBalance(Object...params){
        String sql="update  user set balance = balance-? where uName=?";

        try {
            int i = queryRunner.update(sql, params);
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<User> queryUser(){
        String sql="select * from user ";
        try {
            return findList(User.class,sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public User queryUser(Object...params){
        String sql="select * from user where uName=?";
        try {
            return findBean(User.class,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }



    public static void main(String[] args) {
        UserDao userDao = new UserDao();
//        List<User> users = userDao.queryUsersById("admin", "admin");
//        for (User user : users) {
//            System.out.println(user);
//        }
//         int i = userDao.InsertValues("xg", "123", 0);
//        System.out.println("插入了"+i+"条数据");

//        int i = userDao.updateAddBalance(150,"zxx");
//        System.out.println("更新了"+i+"条数据");

//        int i = userDao.updateReduceBalance(150,"zxx");
//        System.out.println("更新了"+i+"条数据");

        List<User> users = userDao.queryUserByBalance("zxx");
        for (User user : users) {
            System.out.println(user);
        }


    }






}
